package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) {
        UserInfo userInfo=  InputUtil.getInputObject(new UserInfo(),"欢迎登录","登录");
        try(Connection connection=DBUtil.getconnection()) {

            Statement statement=connection.createStatement();
            String sql="SELECT username,password,nickname\n" +
                    "FROM userinfo " +
                    "WHERE  username='"+userInfo.getUsername()+"'AND password='"+userInfo.getPassword()+"'";
            ResultSet rs= statement.executeQuery(sql);
            /*while (rs.next()){
                Scanner scan=new Scanner(System.in);
                System.out.println("请输入用户名");
                String username=scan.nextLine();
                System.out.println("请输入密码");
                String password= scan.nextLine();
                String name=rs.getString("username");
                String pw=rs.getString("password");
                if (name.equals(username) && pw.equals(password)){
                    System.out.println("登陆成功");
                    break;
                }else {
                    System.out.println("登录失败");
                    break;
                }
            }*/
            if (rs.next()){
                System.out.println("登录成功，欢迎你："+rs.getString("nickname"));
            }else {
                System.out.println("登录失败，用户名或密码不正确");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
